<?php


namespace App\Http\Controllers\Api;


use App\Http\Controllers\Controller;
use App\Models\User;
use App\Service\Firebase;
use App\Service\Push;
use Illuminate\Support\Facades\DB;

class NotifyController extends Controller
{
    public function notify()
    {
        $data = json_decode( \request()->getContent() );
//        if (empty($data)){
//            return response()->json( [
//                'error'   => false,
//                'data'   => $data,
//                'message' => '参数未找到！！'
//            ] );
//        }

            // optional payload
//            $registerToken = "fPyu_3WASTafsH1nosT1uk:APA91bECtKQrQ6d2ksKLrlW_tlf33vG65zNGmPbRnroQSPe6QX5fCwHJ7_cLb5WDpMmirfIqABRnOlRwoYsJ5kXbrQjaz61AwGaYh5souP93vLzOYXk9_j0Jz8zxgbKziCr089uWl5Ng";
            $registerToken = [
                             "fPyu_3WASTafsH1nosT1uk:APA91bECtKQrQ6d2ksKLrlW_tlf33vG65zNGmPbRnroQSPe6QX5fCwHJ7_cLb5WDpMmirfIqABRnOlRwoYsJ5kXbrQjaz61AwGaYh5souP93vLzOYXk9_j0Jz8zxgbKziCr089uWl5Ng"
                             ,"fPyu_3WASTafsH1nosT1uk:APA91bECtKQrQ6d2ksKLrlW_tlf33vG65zNGmPbRnroQSPe6QX5fCwHJ7_cLb5WDpMmirfIqABRnOlRwoYsJ5kXbrQjaz61AwGaYh5souP93vLzOYXk9_j0Jz8zxgbKziCr089uWl5Ng"
                             ,"fPyu_3WASTafsH1nosT1uk:APA91bECtKQrQ6d2ksKLrlW_tlf33vG65zNGmPbRnroQSPe6QX5fCwHJ7_cLb5WDpMmirfIqABRnOlRwoYsJ5kXbrQjaz61AwGaYh5souP93vLzOYXk9_j0Jz8zxgbKziCr089uWl5Ng"
                             ,"fPyu_3WASTafsH1nosT1uk:APA91bECtKQrQ6d2ksKLrlW_tlf33vG65zNGmPbRnroQSPe6QX5fCwHJ7_cLb5WDpMmirfIqABRnOlRwoYsJ5kXbrQjaz61AwGaYh5souP93vLzOYXk9_j0Jz8zxgbKziCr089uWl5Ng"
                             ,"fPyu_3WASTafsH1nosT1uk:APA91bECtKQrQ6d2ksKLrlW_tlf33vG65zNGmPbRnroQSPe6QX5fCwHJ7_cLb5WDpMmirfIqABRnOlRwoYsJ5kXbrQjaz61AwGaYh5souP93vLzOYXk9_j0Jz8zxgbKziCr089uWl5Ng"
                             ,"fPyu_3WASTafsH1nosT1uk:APA91bECtKQrQ6d2ksKLrlW_tlf33vG65zNGmPbRnroQSPe6QX5fCwHJ7_cLb5WDpMmirfIqABRnOlRwoYsJ5kXbrQjaz61AwGaYh5souP93vLzOYXk9_j0Jz8zxgbKziCr089uWl5Ng"
                             ,"fPyu_3WASTafsH1nosT1uk:APA91bECtKQrQ6d2ksKLrlW_tlf33vG65zNGmPbRnroQSPe6QX5fCwHJ7_cLb5WDpMmirfIqABRnOlRwoYsJ5kXbrQjaz61AwGaYh5souP93vLzOYXk9_j0Jz8zxgbKziCr089uWl5Ng"
                             ,"fPyu_3WASTafsH1nosT1uk:APA91bECtKQrQ6d2ksKLrlW_tlf33vG65zNGmPbRnroQSPe6QX5fCwHJ7_cLb5WDpMmirfIqABRnOlRwoYsJ5kXbrQjaz61AwGaYh5souP93vLzOYXk9_j0Jz8zxgbKziCr089uWl5Ng"
                             ,"fPyu_3WASTafsH1nosT1uk:APA91bECtKQrQ6d2ksKLrlW_tlf33vG65zNGmPbRnroQSPe6QX5fCwHJ7_cLb5WDpMmirfIqABRnOlRwoYsJ5kXbrQjaz61AwGaYh5souP93vLzOYXk9_j0Jz8zxgbKziCr089uWl5Ng"
                             ,"fPyu_3WASTafsH1nosT1uk:APA91bECtKQrQ6d2ksKLrlW_tlf33vG65zNGmPbRnroQSPe6QX5fCwHJ7_cLb5WDpMmirfIqABRnOlRwoYsJ5kXbrQjaz61AwGaYh5souP93vLzOYXk9_j0Jz8zxgbKziCr089uWl5Ng"
                             ,"fPyu_3WASTafsH1nosT1uk:APA91bECtKQrQ6d2ksKLrlW_tlf33vG65zNGmPbRnroQSPe6QX5fCwHJ7_cLb5WDpMmirfIqABRnOlRwoYsJ5kXbrQjaz61AwGaYh5souP93vLzOYXk9_j0Jz8zxgbKziCr089uWl5Ng"
                             ,"fPyu_3WASTafsH1nosT1uk:APA91bECtKQrQ6d2ksKLrlW_tlf33vG65zNGmPbRnroQSPe6QX5fCwHJ7_cLb5WDpMmirfIqABRnOlRwoYsJ5kXbrQjaz61AwGaYh5souP93vLzOYXk9_j0Jz8zxgbKziCr089uWl5Ng"
                             ,"cP495F1uSZ2H3-GNrq7jY-:APA91bGXVlnD7attDOzetRbgGn6iZUn1XRIdDTLVhPtDpzlSGrSQSErLoPu0H9ZmAOQSOGhhZGLf5rpl3irsUuVKL7Yr1uKvkL8D1k56OHdfmVb06MSnNKbMHBeGXjhLNJ7GAUtbWxEt"
                             ,"cP495F1uSZ2H3-GNrq7jY-:APA91bGXVlnD7attDOzetRbgGn6iZUn1XRIdDTLVhPtDpzlSGrSQSErLoPu0H9ZmAOQSOGhhZGLf5rpl3irsUuVKL7Yr1uKvkL8D1k56OHdfmVb06MSnNKbMHBeGXjhLNJ7GAUtbWxEt"
                             ,"cP495F1uSZ2H3-GNrq7jY-:APA91bGXVlnD7attDOzetRbgGn6iZUn1XRIdDTLVhPtDpzlSGrSQSErLoPu0H9ZmAOQSOGhhZGLf5rpl3irsUuVKL7Yr1uKvkL8D1k56OHdfmVb06MSnNKbMHBeGXjhLNJ7GAUtbWxEt"
                             ,"cP495F1uSZ2H3-GNrq7jY-:APA91bGXVlnD7attDOzetRbgGn6iZUn1XRIdDTLVhPtDpzlSGrSQSErLoPu0H9ZmAOQSOGhhZGLf5rpl3irsUuVKL7Yr1uKvkL8D1k56OHdfmVb06MSnNKbMHBeGXjhLNJ7GAUtbWxEt"
                             ,"cP495F1uSZ2H3-GNrq7jY-:APA91bGXVlnD7attDOzetRbgGn6iZUn1XRIdDTLVhPtDpzlSGrSQSErLoPu0H9ZmAOQSOGhhZGLf5rpl3irsUuVKL7Yr1uKvkL8D1k56OHdfmVb06MSnNKbMHBeGXjhLNJ7GAUtbWxEt"
                             ,"cP495F1uSZ2H3-GNrq7jY-:APA91bGXVlnD7attDOzetRbgGn6iZUn1XRIdDTLVhPtDpzlSGrSQSErLoPu0H9ZmAOQSOGhhZGLf5rpl3irsUuVKL7Yr1uKvkL8D1k56OHdfmVb06MSnNKbMHBeGXjhLNJ7GAUtbWxEt"
                             ,"cP495F1uSZ2H3-GNrq7jY-:APA91bGXVlnD7attDOzetRbgGn6iZUn1XRIdDTLVhPtDpzlSGrSQSErLoPu0H9ZmAOQSOGhhZGLf5rpl3irsUuVKL7Yr1uKvkL8D1k56OHdfmVb06MSnNKbMHBeGXjhLNJ7GAUtbWxEt"
                             ,"cP495F1uSZ2H3-GNrq7jY-:APA91bGXVlnD7attDOzetRbgGn6iZUn1XRIdDTLVhPtDpzlSGrSQSErLoPu0H9ZmAOQSOGhhZGLf5rpl3irsUuVKL7Yr1uKvkL8D1k56OHdfmVb06MSnNKbMHBeGXjhLNJ7GAUtbWxEt"
                             ,"cP495F1uSZ2H3-GNrq7jY-:APA91bGXVlnD7attDOzetRbgGn6iZUn1XRIdDTLVhPtDpzlSGrSQSErLoPu0H9ZmAOQSOGhhZGLf5rpl3irsUuVKL7Yr1uKvkL8D1k56OHdfmVb06MSnNKbMHBeGXjhLNJ7GAUtbWxEt"
                             ,"cP495F1uSZ2H3-GNrq7jY-:APA91bGXVlnD7attDOzetRbgGn6iZUn1XRIdDTLVhPtDpzlSGrSQSErLoPu0H9ZmAOQSOGhhZGLf5rpl3irsUuVKL7Yr1uKvkL8D1k56OHdfmVb06MSnNKbMHBeGXjhLNJ7GAUtbWxEt"
                             ,"cP495F1uSZ2H3-GNrq7jY-:APA91bGXVlnD7attDOzetRbgGn6iZUn1XRIdDTLVhPtDpzlSGrSQSErLoPu0H9ZmAOQSOGhhZGLf5rpl3irsUuVKL7Yr1uKvkL8D1k56OHdfmVb06MSnNKbMHBeGXjhLNJ7GAUtbWxEt"
                             ,"cP495F1uSZ2H3-GNrq7jY-:APA91bGXVlnD7attDOzetRbgGn6iZUn1XRIdDTLVhPtDpzlSGrSQSErLoPu0H9ZmAOQSOGhhZGLf5rpl3irsUuVKL7Yr1uKvkL8D1k56OHdfmVb06MSnNKbMHBeGXjhLNJ7GAUtbWxEt"
                             ,"cP495F1uSZ2H3-GNrq7jY-:APA91bGXVlnD7attDOzetRbgGn6iZUn1XRIdDTLVhPtDpzlSGrSQSErLoPu0H9ZmAOQSOGhhZGLf5rpl3irsUuVKL7Yr1uKvkL8D1k56OHdfmVb06MSnNKbMHBeGXjhLNJ7GAUtbWxEt"

            ];

            $payload = $data->payload ?? [
                'aps' => [
                    'mutable-content' => 1,
                    'category' => 'NEW_MESSAGE_CATEGORY'
                ],];
            $title = $data->title ?? 'this is a test-title';
            $message = $data->message ?? 'test--milo';            // notification message
            $push_type = $data->push_type ?? 'individual';            // push type - single user / topic
            $body = $data->body ?? '这是一个测试body!!!!';

            $push = new Push();
            $firebase = new Firebase();
        try {
            $push->setTitle( $title )
                ->setMessage( $message )
                ->setPayload( $payload )
                ->setData($body);
            $response = '';
            $json     = $push->getPush();

            if ( $push_type === 'topic' ) {
                $response = $firebase->sendToTopic( 'global', $json );
            } else if ( $push_type === 'individual' ) {
                $regId    = $registerToken ?? '';
                $response = $firebase->sendMultiple( $regId, $json );
            }

            return response()->json( [
                'response' => $response
            ] );
        } catch ( \Exception $ex ) {
            return response()->json( [
                'error'   => true,
                'message' => $ex->getMessage()
            ] );
        }
    }
}